package session.stateless;

import entity.IndependentVariable;
import entity.Product;
import entity.PurchaseItem;
import entity.SalesHistory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class SalesHistorySessionBean implements SalesHistorySessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void addSalesHistory(PurchaseItem purchaseItem) {
        DateFormat df;
        df = new SimpleDateFormat("dd-MM-yyyy");
        String temp = df.format(purchaseItem.getRequestDate());
        String str[] = temp.split("-");
        Integer month = Integer.parseInt(str[1]);
        Integer year = Integer.parseInt(str[2]);
        SalesHistory salesHistory = getSalesHistory(month, year, purchaseItem.getProduct().getCode());
        if (salesHistory == null) {
            Product product = entityManager.find(Product.class, purchaseItem.getProduct().getCode());


            salesHistory = new SalesHistory(month, year, product, purchaseItem.getQuantity());
            PurchaseItem poi = entityManager.find(PurchaseItem.class, purchaseItem.getId());
            salesHistory.getPurchaseItems().add(poi);
            entityManager.persist(salesHistory);
            entityManager.flush();
        } else {
            SalesHistory sh = entityManager.find(SalesHistory.class, salesHistory.getId());
            Integer newQuantity = sh.getQuantity() + purchaseItem.getQuantity();


            sh.setQuantity(newQuantity);

            PurchaseItem poi = entityManager.find(PurchaseItem.class, purchaseItem.getId());
            salesHistory.getPurchaseItems().add(poi);
            entityManager.persist(sh);
            entityManager.flush();
        }

    }

    @Override
    public SalesHistory getSalesHistory(Integer month, Integer year, String code) {


        Query query = entityManager.createQuery("SELECT u FROM SalesHistory u WHERE u.salesMonth = :inmonth AND u.salesYear= :inyear AND u.product.code=:incode");
        query.setParameter("inmonth", month);
        query.setParameter("inyear", year);
        query.setParameter("incode", code);
        SalesHistory salesHistory = null;

        try {
            salesHistory = (SalesHistory) query.getSingleResult();
        } catch (Exception ex) {
        }
        return salesHistory;

    }

    @Override
    public ArrayList<SalesHistory> getSalesHistories(String code) {


        Query query = entityManager.createQuery("SELECT u FROM SalesHistory u WHERE u.product.code=:incode");
        query.setParameter("incode", code);
        ArrayList<SalesHistory> salesHistories = new ArrayList<SalesHistory>();


        for (Object o : query.getResultList()) {
            salesHistories.add((SalesHistory) o);
        }

        Collections.sort(salesHistories);
        return salesHistories;
    }

    public double round(double valueToRound, int numberOfDecimalPlaces) {
        double multiplicationFactor = Math.pow(10, numberOfDecimalPlaces);
        double interestedInZeroDPs = valueToRound * multiplicationFactor;

        return Math.round(interestedInZeroDPs) / multiplicationFactor;
    }

    @Override
    public ArrayList<SalesHistory> getSalesHistories(ArrayList<IndependentVariable> independentVariables, String code) {
        ArrayList<SalesHistory> salesHistories = new ArrayList<SalesHistory>();
        for (IndependentVariable idv : independentVariables) {
            SalesHistory sh = getSalesHistory(idv.getCollectedMonth(), idv.getCollectedYear(), code);
            if (sh != null) {
                salesHistories.add(sh);
            }
        }
        Collections.sort(salesHistories);
        return salesHistories;
    }
}
